<template>
  <el-card border>
    <el-table :data="skuArr" border stripe>
      <el-table-column label="序号" type="index" width="80px" />
      <el-table-column label="名称" prop="skuName" show-overflow-tooltip width="150px" />
      <el-table-column label="描述" prop="skuDesc" show-overflow-tooltip />
      <el-table-column label="图片" width="150px">
        <template #default="{ row }">
          <img :src="row.skuDefaultImg" style="height: 100xp; width: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight" width="150px" />
      <el-table-column label="价格" prop="price" width="150px" />
      <el-table-column label="操作" width="250px" fixed="right">
        <template #default="{ row }">
          <el-button v-has="'btn.Sku.update'" :type="row.isSale == 1 ? 'info' : 'success'" :icon="row.isSale == 1 ? 'bottom' : 'Top'" @click="updateSale(row)" />
          <el-button v-has="'btn.Sku.update'" type="primary" icon="Edit" @click="updateSku" />
          <el-button v-has="'btn.Sku.detail'" type="info" icon="InfoFilled" @click="findSku(row)" />
          <el-popconfirm :title="`确定删除${row.skuName}吗？`" width="200px" @confirm="removeSku(row.id)">
            <template #reference>
              <el-button v-has="'btn.Sku.remove'" type="danger" icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40]" layout=" prev, pager, next, jumper,->, sizes,total" :total="total" background style="margin-top: 10px" @change="getSkuList" />

    <!-- 抽屉 展示商品详情 -->
    <el-drawer v-model="drawerVisible" title="查看商品详情" size="30%" direction="rtl">
      <el-descriptions :label-width="100" :column="1">
        <el-descriptions-item label="商品名称：" label-align="center">{{ skuInfo.skuName }}</el-descriptions-item>
        <el-descriptions-item label="商品描述：" label-align="center">{{ skuInfo.skuDesc }}</el-descriptions-item>
        <el-descriptions-item label="商品价格：" label-align="center">{{ skuInfo.price }}</el-descriptions-item>
        <el-descriptions-item label="平台属性：" label-align="center">
          <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item" type="success" style="margin-right: 10px">{{ item.valueName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="销售属性：" label-align="center">
          <el-tag v-for="item in skuInfo.skuSaleAttrValueList" :key="item" type="warning" style="margin-right: 10px">{{ item.saleAttrValueName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="商品图片">
          <el-carousel :interval="4000" type="card" height="200px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <!-- <img :src="item.imgUrl" alt="" /> -->
              <el-image :src="item.imgUrl" fit="scale-down" :lazy="true" style="width: 100%; height: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { reqSkuList, reqSaleSku, reqCancelSaleSku, reqSkuInfo, reqDeleteSku } from '@/api/product/sku';
import type { SkuResponseData, SkuData, SkuInfoData } from '@/api/product/sku/type';
import { ElMessage } from 'element-plus';
onMounted(() => {
  getSkuList();
});

let pageNo = ref<number>(1);
let pageSize = ref<number>(10);
let lastPageSize = ref<number>(10);
let total = ref<number>(100);
//抽屉控制
let drawerVisible = ref<boolean>(false);

let skuArr = ref<SkuData[]>([]);

const getSkuList = async () => {
  if (pageSize.value !== lastPageSize.value) {
    pageNo.value = 1;
    lastPageSize.value = pageSize.value;
  }
  const result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value);
  if (result.code === 200) {
    total.value = result.data.total;
    skuArr.value = result.data.records;
  }
};

// sku上架下架
const updateSale = async (row: SkuData) => {
  let result;
  if (row.isSale === 1) {
    result = await reqCancelSaleSku(row.id as number);
  } else {
    result = await reqSaleSku(row.id as number);
  }
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: row.isSale === 1 ? '下架成功' : '上架成功',
    });
    getSkuList();
  }
};
// sku更新
const updateSku = () => {
  ElMessage({
    type: 'success',
    message: '程序员努力更新中...',
  });
};

let skuInfo = ref<SkuData>({});

let loading = ref<boolean>(false);
//查看商品详情
const findSku = async (row: SkuData) => {
  if (loading.value) return;
  loading.value = true;
  drawerVisible.value = true;

  let result: SkuInfoData = await reqSkuInfo(row.id as number);
  if (result.code === 200) {
    skuInfo.value = result.data;
  }
  // currentSku.value = row;
  loading.value = false;
};

const removeSku = async (id: number) => {
  const result = await reqDeleteSku(id);
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    });
    if (skuArr.value.length === 1 && pageNo.value !== 1) {
      pageNo.value -= 1;
    }
    getSkuList();
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    });
  }
};
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
